﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DotNetNuke.Tests.UI.WatiN.Common;
using System.Configuration;
using System.Web.Configuration;
using DotNetNuke.Tests.UI.WatiN.Utilities;
using System.IO;
using Gallio.Framework.Pattern;
using WatiN.Core;

namespace DotNetNuke.Tests.UI.WatiN.Newsletters
{
    using Common;
    using Common.WatiNObjects;

    using MbUnit.Framework;

    using Utilities;

    [TestFixture]
    public class NewsletterTests : TestBase
    {
        #region Private Members

        private const string AdditionalEmailsField = "additional@dnncorp.com";
        private const string FromAddressField = "from@dnncorp.com";
        private const string ReplyToField = "reply@dnncorp.com";
        private const string SubjectField = "New Newsletter Subject";
        private IE _instance;
        private NewsletterPage _newsletterPage;
        #endregion

        #region SetUp and TearDown

        [SetUp]
        public void SetUp()
        {
            foreach (var file in Directory.GetFiles(TestEnvironment.TestEmailPath))
            {
                File.Delete(file);
            }

            WebConfigManager.UpdateConfig(TestEnvironment.WebsitePath);
            _instance = WatiNUtil.OpenIEInstance(SiteURL, SilentMode, IETimeOut, AutoCloseIE, AutoMovePointer);
            var homePage = new WatiNBase(_instance, SiteURL);
            var loginPage = new LoginPage(homePage);
            loginPage.LoginUser(TestUsers.AdminUpdatedPassword.UserName, TestUsers.AdminUpdatedPassword.Password);
            _instance.GoTo(SiteURL + "/Admin/Newsletters.aspx");
            _newsletterPage = new NewsletterPage(homePage);
        }

        [TearDown]
        public void TearDown()
        {
            _instance.GoTo(SiteURL + "/LogOff.aspx");
            WatiNUtil.CloseIEInstance(_instance);
        }

        #endregion

        [Test]
        public void Send_To_One_Additional_Address_Sync_Sends_One_Email()
        {
            _newsletterPage.AdditionalEmailsField.Value = AdditionalEmailsField;
            _newsletterPage.FromAddressField.Value = FromAddressField;
            _newsletterPage.ReplyToField.Value = ReplyToField;
            _newsletterPage.SubjectField.Value = SubjectField;
            _newsletterPage.SelectRadioButtonByName(_instance, "BASIC");
            _newsletterPage.SelectRadioButtonByName(_instance, "S");
            _newsletterPage.ContentTextBox.Value = "This is the Newsletter";
            _newsletterPage.SendButton.Click();
            Assert.IsTrue(Directory.GetFiles(TestEnvironment.TestEmailPath).Length == 1);
        }

        [Test]
        public void Send_To_One_Additional_Address_Async_Sends_Two_Emails()
        {
            _newsletterPage.AdditionalEmailsField.Value = AdditionalEmailsField;
            _newsletterPage.FromAddressField.Value = FromAddressField;
            _newsletterPage.ReplyToField.Value = ReplyToField;
            _newsletterPage.SubjectField.Value = SubjectField;
            _newsletterPage.SelectRadioButtonByName(_instance, "BASIC");
            _newsletterPage.ContentTextBox.Value = "This is the Newsletter";
            _newsletterPage.SendButton.Click();
            System.Threading.Thread.Sleep(5000);
            Assert.IsTrue(Directory.GetFiles(TestEnvironment.TestEmailPath).Length == 2);
        }

        [Test]
        public void Send_Sync_Sends_To_Correct_Addresses()
        {
            _newsletterPage.AdditionalEmailsField.Value = AdditionalEmailsField;
            _newsletterPage.FromAddressField.Value = FromAddressField;
            _newsletterPage.ReplyToField.Value = ReplyToField;
            _newsletterPage.SubjectField.Value = SubjectField;
            _newsletterPage.SelectRadioButtonByName(_instance, "BASIC");
            _newsletterPage.ContentTextBox.Value = "This is the Newsletter";
            _newsletterPage.SelectRadioButtonByName(_instance, "S");
            _newsletterPage.SendButton.Click();

            var emailFileName = Directory.GetFiles(@TestEnvironment.TestEmailPath)[0];
            var emailContents = File.OpenText(emailFileName);
            var emailLine = emailContents.ReadLine();
            var emailLines = new List<string>();
            while (emailLine != null)
            {
                emailLines.Add(emailLine);
                emailLine = emailContents.ReadLine();
            }

            var fromLine = emailLines.Find(l => l.Contains("From:"));
            var toLine = emailLines.Find(l => l.Contains("To:"));
            var replyToLine = emailLines.Find(l => l.Contains("Reply-To:"));
            var subjectLine = emailLines.Find(l => l.Contains("Subject:"));

            Assert.Contains(fromLine, FromAddressField);
            Assert.Contains(toLine, AdditionalEmailsField);
            //Assert.Contains(subjectLine, SubjectField);
            Assert.Contains(replyToLine, ReplyToField);
            //http://support.dotnetnuke.com/issue/ViewIssue.aspx?id=11507&PROJID=2
        }
    }
}
